package com.cyh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyh.mapper.NbaPlayerMapper;
import com.cyh.pojo.NbaPlayer;
import com.cyh.pojo.vo.HitRateVo;
import com.cyh.pojo.vo.PlayerTimeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: cyh
 * @Date: 2022/6/15 23:19
 * @Description: NbaPlayerController
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/player")
public class NbaPlayerController {
    @Resource
    private NbaPlayerMapper nbaPlayerMapper;

    /*NBA前十球员出场数*/
    @RequestMapping("/list")
    public List<HitRateVo> findAll() {
        List<HitRateVo> hitRateVoList = new ArrayList<>();
        QueryWrapper<NbaPlayer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("player", "session", "hit_rate", "three_rate", "score");

        queryWrapper.last("limit 10");
        List<NbaPlayer> dataList = nbaPlayerMapper.selectList(queryWrapper);
        for (int i = 0; i < dataList.size(); i++) {
            NbaPlayer data = dataList.get(i);
            String player = data.getPlayer();
            Integer session = data.getSession();
            String hitRate = data.getHitRate();
            String threeRate = data.getThreeRate();
            Float score = data.getScore();

            String replace = hitRate.replace("%", "");
            Float ad = Float.parseFloat(replace) / 100;
            DecimalFormat df = new DecimalFormat("0.000");
            String format = df.format(ad);

            String hitThree1 = threeRate.replace("%", "");
            Float hitThree2 = Float.parseFloat(hitThree1) / 100;
            String format2 = df.format(hitThree2);

            HitRateVo hitRateVo = new HitRateVo();
            hitRateVo.setScores(score);
            hitRateVo.setHitRate(format);
            hitRateVo.setThreeRate(format2);
            hitRateVo.setPlayer(player);
            hitRateVo.setSession(session);
            hitRateVoList.add(hitRateVo);
        }
        return hitRateVoList;
    }

    @RequestMapping("/playingTime")
    public List<PlayerTimeVo> playingTime() {
        List<PlayerTimeVo> playerTimeVoList = new ArrayList<>();
        QueryWrapper<NbaPlayer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("player", "session", "playing_time");
        queryWrapper.last("limit 10");
        List<NbaPlayer> dataList = nbaPlayerMapper.selectList(queryWrapper);
        for (int i = 0; i < dataList.size(); i++) {
            NbaPlayer data = dataList.get(i);
            PlayerTimeVo playerTimeVo = new PlayerTimeVo();
            BeanUtils.copyProperties(data, playerTimeVo);
            playerTimeVoList.add(playerTimeVo);
        }
        return playerTimeVoList;
    }

    @RequestMapping("/rankingList")
    public List<NbaPlayer> getRankingList() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.last("limit 10");
        List<NbaPlayer> nbaPlayers = nbaPlayerMapper.selectList(queryWrapper);
        return nbaPlayers;
    }

}
